<?php
/**
*Mappers act as a proxy to the persistent layer. Each Mapper class makes a decision on how to *handle a particular Domain Object ( ie don't save, save to DB or Session')
*
*@package Maps
*@author Garry Willits
*@copyright 2005
*@version 1.1
*/

class ObjectMapper extends Mapper{
	
protected $controller;

protected static $type = "objectmapper";

/**
*@var string
*/
protected $object_type = "Object";

/**
*@var int
*/
protected $max = 15;
/**
*@var array
*/
protected $terms;

protected $model;
/**
*Instantiate the QueryManager
*@see QueryManager
*/
protected function __construct(){
	$this->controller = CI_Controller::get_instance();
	$this->model = $this->controller->load->model('DaoModel','daomodel');
}
/**
 * Get the singleton of this mapper
 * Enter description here ...
 */
public static function GetInstance(){
	return Mapper::doGetInstance( ObjectMapper::$type );
}

/**
*Find an object or set of objects
*@param int or array of ints
*@param string;
*/
public function doFind( $terms ){

}
/**
*get ids of next set of objects
*@return array
*/
public function next(  ){

}
/**
*insert object
*@param DomainObject
*/
public function insert( $object ){
	$class = get_class( $object );
	$proptypes = $object->GetPropertyTypes();
	if( !$this->doInsert($class,$object->GetProperties() ) ){
		// Maybe the table hasn't been created
		if( !$this->controller->daomodel->TableExists( $class ) ){
			$this->controller->daomodel->CreateDAO( $class, $proptypes );
		}
		// ok not that? maybe a column has changed in the definition
		$validatedata = $this->controller->daomodel->ValidateTableTooMany( $class,$proptypes );
		if( is_array( $validatedata ) ){
			$add_data = array();
			foreach ( $validatedata as $prop ){
				$add_data[$prop] = $proptypes[ $prop];
			}
			$this->controller->daomodel->AddFields( $class,$add_data );
		}
		// Check that we haven't removed a column
		$validatedata2 = $this->controller->daomodel->ValidateTableTooFew( $class,$proptypes );
		if( is_array( $validatedata2 ) ){
			$this->controller->daomodel->RemoveFields( $class,$validatedata2 );
		}
		//
		// try again
		$this->doInsert($class,$object->GetProperties() );
	}	

}

public function GetID( $class ){
	return $this->controller->daomodel->GetOID( $class );
}
protected function doInsert( $class,$properties ){
	// need to remove the childoids
	unset($properties['childoids']);
	$properties = $this->replaceObjects( $properties );
	return $this->controller->daomodel->save( $class,$properties ) ;
}


protected function replaceObjects( &$properties ){
	foreach( $properties as &$property ){
		if( $property instanceof DomainObject ){
			$property = "oid_".$property->oid;
		}
	}
	return $properties;
}

/**
*update object
*@param DomainObject
*/
public function update( DomainObject $object ){
	$class = get_class( $object );
	$properties = $object->GetChangedProperties();
	return $this->controller->daomodel->update( $class,$object->oid,$properties) ;
}
/**
 * 
 * Load a child object by id
 * @param unknown_type $oid
 */
public function GetByIds( $oids ){
	$retDat = $this->controller->daomodel->LoadAll( $oids );
	$retArray = array();
	foreach( $retDat as $key => $val ){
		foreach ($val as $object){
			$retArray[]= new $key( $object );
		}
	}
	return $retArray;
}
/**
*load object
*@param int
*/
public function load( $object ){
	$dat = $this->controller->daomodel->Load( get_class( $object ),$object->oid );
	$this->LoadProperties($object, $dat[0] );

}
/**
 * 
 * Loads in properties from stdClass
 * @param unknown_type $object
 * @param unknown_type $props
 */
public function LoadProperties( DomainObject $object,stdClass $props ){
	$object->addProperties( $props );
}
/**
 * 
 * Load an object from the id
 * @param unknown_type $oid
 */
public function loadObject($oid){
	return $this->controller->daomodel->loadObject( $oid );
}
/**
*insert object
*@param DomainObject
*/
public function delete( $object ){

}
/**
*set max number to cache
*@param int
*/
public function setMax( $max ){
$this->max=$max;
}
/**
*Returns limit of records
*@return int
*/
public function getLimit(  ){
return $this->limit;
}
}
?>
